/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.osgg.daos;

import java.io.Serializable;
import java.util.List;
import net.osgg.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author omar
 */
public abstract class GenericDAOImpl<T, ID extends Serializable> implements GenericDAO<T, ID> {
 
    protected Session getSession() {
        return HibernateUtil.getSession();
    }
 
    public void nuevo(T entity) {
        Session hibernateSession = this.getSession();
        hibernateSession.saveOrUpdate(entity);
    }
 
    public void actualizar(T entity) {
        Session hibernateSession = this.getSession();
        hibernateSession.merge(entity);
    }
 
    public void borrar(T entity) {
        Session hibernateSession = this.getSession();
        hibernateSession.delete(entity);
    }
 
    public List<T> buscarPredeterminado(Query query) {
        List<T> t;
        t = (List<T>) query.list();
        return t;
    }
 
    public List buscarTodos(Query query) {
        List T=null;
        T = query.list();
        return T;
    }
 
    public T buscarPorID(Class clazz, Integer id) {
        Session hibernateSession = this.getSession();
        T t = null;
        t = (T) hibernateSession.get(clazz, id);
        return t;
    }
 
    public List devolverTodos(Class clazz) {
        Session hibernateSession = this.getSession();
        List T = null;
        Query query = hibernateSession.createQuery("from " + clazz.getName());
        T = query.list();
        return T;
    }
}
